#include "UserDialog.h"
#include <iostream>

UserDialog::UserDialog(User *user)
{
    this->user = user;

    layout_main = new QGridLayout;

    label_list = new QVector<QLabel*>;

}

UserDialog::~UserDialog()
{
    for (QVector<QLabel*>::const_iterator iter = label_list->begin();iter != label_list->end(); iter++)
    {
        delete *iter;
    }
    delete label_list;

    delete layout_main;
}

void UserDialog::init_view_main()
{
    setFixedSize(240, 160);
    setWindowTitle("用户");

    layout_main->setSpacing(2);    //设置控件距离
    setLayout(layout_main);

    for(int i = 0; i < user->attr_count() - 1; i++)
    {
        QLabel *label1 = new QLabel;
        QLabel *label2 = new QLabel;

        label1->setStyleSheet("QLabel{font:12px;padding: 5px 15px 5px 15px;}");
        label2->setStyleSheet("QLabel{font:12px;padding: 5px 15px 5px 15px;}");

        switch (i) {
        case 0:
            label1->setText("id：");
            label2->setText(QString::number(user->get_id()));
            break;
        case 1:
            label1->setText("用户名：");
            label2->setText(user->get_name());
            break;
        case 2:
            label1->setText("创建时间：");
            label2->setText(user->get_create_time().left(10));
            break;
        case 3:
            label1->setText("身份：");
            QString authority = user->get_authority() == 0 ? "管理员" : "普通用户";
            label2->setText(authority);
            break;
        }

        layout_main->addWidget(label1, i, 0);
        layout_main->addWidget(label2, i, 1);

        label_list->push_back(label1);
        label_list->push_back(label2);
    }

}
